<?php

class DetailedUserRestResource extends RestResource {

    private static $instance;

    protected function __construct() {
        parent::__construct('DetailedUser', '/detailed-users');
    }

    /**
     * @return DetailedUserRestResource
     */
    public static function singleton() {
        if (!isset(self::$instance)) {
            self::$instance = new DetailedUserRestResource();
        }
        return self::$instance;
    }

    public function search($from, $to, $requestStatus = array()) {
        $parameters = array(
            'from' => $from->format('m/d/Y'),
            'to' => $to->format('m/d/Y'),
            'requestStatus' => $requestStatus,
        );
        $parameters = $this->serializeParameters($parameters);
        $b = YougoRestClient::singleton()->get($this->resourcePath . $parameters);
        return $this->jsonToObjectList($b->getResponseText());
    }

    /**
     * Necessary to handle multiple values in url encoded query param...
     * @param <type> $parameters
     * @return <type>
     */
    private function serializeParameters($parameters) {
        $serializedParameters = '?';
        foreach ($parameters as $key => $value) {
            if (is_array($value)) {
                foreach ($value as $val) {
                    $serializedParameters .= $key . '=' . $val . '&';
                }
            } else {
                if ($value != '')
                    $serializedParameters .= $key . '=' . $value . '&';
            }
        }
        return substr($serializedParameters, 0, strlen($serializedParameters) - 1);
    }

}